package com.acheli.registry.entity.render;

import com.acheli.ACHeli;
import com.acheli.registry.config.CustomDataBus;
import com.acheli.registry.entity.model.CustomGeckolibModel;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.factory.SafetyFactoryProvider;
import com.acheli.rideable.factory.ACHeliEntitiesFactory;
import com.acheli.rideable.factory.gson.GsonEntitiesFactory;
import com.acheli.rideable.functionLathes.RideableFL;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.renderer.GeoEntityRenderer;

import static com.acheli.ACHeli.MODID;


public class RideableEntityRender extends GeoEntityRenderer<RideableEntity> {
    private EntityRendererProvider.Context context;
    private boolean lateInited = false;

    public RideableEntityRender(EntityRendererProvider.Context context) {
        super(context, new CustomGeckolibModel<>());
        this.context = context;
    }

    @Override
    public boolean shouldRender(RideableEntity entity, Frustum camera, double camX, double camY, double camZ) {
        return super.shouldRender(entity, camera, camX, camY, camZ) || entity.doFactory(new SafetyFactoryProvider<ACHeliEntitiesFactory<RideableEntity, RideableFL>>() {
            @Override
            public void getFct(ACHeliEntitiesFactory<RideableEntity, RideableFL> factoryNotNull) {
                factoryNotNull.getProduct().shouldRender();
            }
        });
    }

    @Override
    public void render(RideableEntity entity, float yaw, float partialTick,
                       @NotNull PoseStack poseStack, @NotNull MultiBufferSource buffer, int packedLight) {
        CustomGeckolibModel<RideableEntity> translatedModel = ((CustomGeckolibModel<RideableEntity>) model);

        entity.doFactory((factory) -> {
            {
                if (entity.factory instanceof GsonEntitiesFactory.GsonRideable gsonFactory) {
                    if (! lateInited) {
                        ACHeli.reloadVehicles();
                        lateInited = true;
                    }

                    translatedModel.setUp(
                            gsonFactory.config.model,
                            gsonFactory.config.texture,
                            gsonFactory.config.animation
                    );
                    if (! translatedModel.shouldCancel()) {
                        super.render(entity,yaw,partialTick,poseStack,buffer,packedLight);
                    }
                }
                factory.getProductFunction().render(context, entity, yaw, partialTick, poseStack, buffer, packedLight);
                factory.getProductFunction().renderTick(poseStack, partialTick);

            }
        });

    }
}